package com.zjg.vue_cms.user.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWTUtil;
import com.zjg.vue_cms.common.Result;
import com.zjg.vue_cms.user.dto.LoginDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author guass
 * @date 2022年04月16日 11:01
 */
@CrossOrigin
@RestController
@RequestMapping("/user/login")
@Slf4j
public class LoginController {

    @PostMapping
    public Result<LoginDto> login(HttpServletRequest request, @RequestBody JSONObject info) {

        log.info("guass {}", info);

        Map<String, Object> map = new HashMap<>();

        map.put("username", info.get("name"));
        map.put("time", System.currentTimeMillis() + 1000 * 60 * 30L);

        String token = JWTUtil.createToken(map, "123".getBytes(StandardCharsets.UTF_8));

        LoginDto loginDto = new LoginDto();
        loginDto.setToken(token);
        loginDto.setId(1000L);

        return Result.success(loginDto);
    }

}
